package com.daykw.wenshu.task;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.daykw.wenshu.config.NutzDaoConfig;
import com.daykw.wenshu.model.SearchAdhocUrl;
import com.daykw.wenshu.utils.MD5Util;
import com.daykw.wenshu.utils.SpringContextHolder;

public class AdhocDiffJob {

	private static Logger logger = Logger.getLogger(AdhocDiffJob.class);

	public void run() {
		try {
			NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);
			List<Record> list= dao.query("t_wenshu_case_type", null);
		//	String[] list = { "吉林省长春林区中级法院", "红石林区基层法院", "白石山林区基层法院", "临江林区基层法院", "抚松林区基层法院", "江源林区基层法院"};
			for (Record key : list) {
				String k = key.getString("type");
				init(k);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void init(String key) throws ParseException, UnsupportedEncodingException {
		NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);
		String start = "2018-01-01";
		String end = "2018-12-21";
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
		long day = this.getDaySub(start, end);

		Date date = format.parse(start);
		LocalDateTime startDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
	
		String name = URLEncoder.encode(key, "utf-8");
		String url1 = "http://wenshu.court.gov.cn/list/list/?sorttype=1&number=&conditions=searchWord+++{0}%20TO%20{1}+%E4%B8%8A%E4%BC%A0%E6%97%A5%E6%9C%9F:{0}%20TO%20{1}&conditions=searchWord+%E5%90%89%E6%9E%97%E7%9C%81+++%E6%B3%95%E9%99%A2%E5%9C%B0%E5%9F%9F:%E5%90%89%E6%9E%97%E7%9C%81&conditions=searchWord+"+name+"+++%E5%85%B3%E9%94%AE%E8%AF%8D:"+name+"";
		// String url =
		// "http://wenshu.court.gov.cn/list/list/?sorttype=1&conditions=searchWord+++{0}%20TO%20{1}+%E4%B8%8A%E4%BC%A0%E6%97%A5%E6%9C%9F:{0}%20TO%20{1}&conditions=searchWord+%E5%92%8C%E9%BE%99%E5%B8%82%E4%BA%BA%E6%B0%91%E6%B3%95%E9%99%A2+++%E5%9F%BA%E5%B1%82%E6%B3%95%E9%99%A2:%E5%92%8C%E9%BE%99%E5%B8%82%E4%BA%BA%E6%B0%91%E6%B3%95%E9%99%A2";
		for (int i = 0; i < day; i=i+2) {
			LocalDateTime startdate = startDateTime.plusDays(i);
			LocalDateTime enddate = startDateTime.plusDays(i+2);
			// Date sdate = this.localDateTime2Date(ldt);
			String s = startdate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
			String e = enddate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
			String value = MessageFormat.format(url1, s, e);

			SearchAdhocUrl search = new SearchAdhocUrl();
			search.setCreate_date(new Date());
			search.setPage(1);
			search.setUrl(value);
			search.setStatus(0);
			search.setStart_date(s);
			search.setEnd_date(s);
			search.setKeyword(key);
			
			String md5 = MD5Util.getMD5(value);
			search.setMd5(md5);
			try {
				dao.fastInsert(search);
				logger.info("完成" + s + ",name:" + search.getKeyword());
			} catch (Exception e1) {
				logger.error("", e1);
			}

		}
	}

	public Date localDateTime2Date(LocalDateTime localDateTime) {
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zdt = localDateTime.atZone(zoneId);
		Date date = Date.from(zdt.toInstant());
		return date;
	}

	public long getDaySub(String beginDateStr, String endDateStr) {
		long day = 0;
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
		java.util.Date beginDate;
		java.util.Date endDate;
		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
			day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
			// System.out.println("相隔的天数="+day);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return day;
	}

}
